package com.LeeCode;

/**
 * 改变一个整数能得到的最大差值
 */

public class Code1432 {
    public static void main(String[] args) {
        int num = 111;
        System.out.println(new Code1432().maxDiff(num));
    }

    public int maxDiff(int num) {
        String numStr = String.valueOf(num);
        char[] chars = numStr.toCharArray();

        int maxNum = num;
        for (char c : chars) {
            if (c != '9') {
                maxNum = Integer.parseInt(numStr.replace(c, '9'));
                break;
            }
        }
        int minNum = num;
        if (chars[0] == '1') {
            for (int i = 1; i < chars.length; i++) {
                if (chars[i] != '0' && chars[i] != chars[0]) {
                    minNum = Integer.parseInt(numStr.replace(chars[i], '0'));
                    break;
                }
            }
        } else {
            minNum = Integer.parseInt(numStr.replace(chars[0], '1'));
        }

        return maxNum - minNum;
    }
}
